layui.define(['table', 'form', 'laydate'], function(exports){
  var $ = layui.$,table = layui.table,form = layui.form, laydate = layui.laydate;

    laydate.render({
        elem: '#createDay'
        , type: 'date'
        , range: false
        , trigger: 'click'
    });

  table.render({
    elem: '#LAY-table'
    ,url: layui.setter.webRoot + '/member/transfer_money_order/list.data'
    ,method: 'post'
    ,parseData: function (res) {
        return {
            "code": res.code,
            "msg": res.msg,
            "count": res.body.total,
            "data": res.body.data
        };
    }
    ,response: {statusCode: 200}
    ,cols: [[
      {field: 'tradeNo', title: '提现订单'}
      ,{field: 'memberId',title: '提现会员ID'}
      ,{field: 'memberName', title: '提现会员',templet:function (record){
              return record.memberName + '('+record.memberPhone+')';
          }}
      ,{field: 'transferTypeName',title: '转账类型'}
      ,{field: 'receivingTypeName',title: '收款类型'}
      ,{field: 'moneyStr',title: '提现金额'}
      ,{field: 'receiptMoneyStr',title: '实际到帐',templet:function (record){
              return record.receiptMoneyStr + '(手续费:'+record.profitProportion+'%)';
          }}
      ,{field: 'stateName',title: '订单状态'}
      ,{field: 'handleTypeName',title: '处理方式'}
      ,{field: 'createDate', title: '创建时间'}
      ,{title: '操作', width: 160, align:'center', fixed: 'right', templet:function(record){
          var html = '';
          html = html + '<a class="layui-btn layui-btn-xs" lay-event="info">查看</a>';
          if(record.state == 1) {
              html = html + '<a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="handle">处理</a>';
          }
          if(record.state == 1 || record.state == 2) {
              html = html + '<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="reject">驳回</a>';
          }
          return html;
      }}
    ]]
    ,page: true
    ,limit: 30
    ,height: 'full-220'
    ,text: '对不起，加载出现异常！'
  });
  
  //监听搜索
  form.on('submit(LAY-search)', function(data){
    var field = data.field;
    //执行重载
    table.reload('LAY-table', {
      where: field
    });
  });
  
  //事件
  var active = {
      info:function (obj){
        layer.open({
            type: 2
            , title: '查看提现申请'
            , content: layui.setter.webRoot + '/member/transfer_money_order/info.html?tradeNo=' + obj.data.tradeNo
            , maxmin: true
            , area: ['90%', '90%']
        })
      }
      ,handle:function (obj){
          layer.open({
              type: 2
              , title: '处理提现申请'
              , content: layui.setter.webRoot + '/member/transfer_money_order/handle.html?tradeNo=' + obj.data.tradeNo
              , maxmin: true
              , area: ['90%', '80%']
              ,btn: ['确定', '取消']
              , yes: function(index, layero){
                  var iframeWindow = window['layui-layer-iframe'+ index]
                      ,submitID = 'LAY-form-submit'
                      ,submit = layero.find('iframe').contents().find('#'+ submitID);
                  iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
                      var field = data.field;
                      $.ajax({
                          type: "post",
                          url: layui.setter.webRoot + '/member/transfer_money_order/success.do',
                          data:field,
                          dataType:"json",
                          success:function(res){
                              if(res.code == 200){
                                  table.reload('LAY-table');
                                  layer.close(index);
                              }else{
                                  layer.msg(res.msg,{icon: 2});
                              }
                          }
                      });
                  });
                  submit.trigger('click');
              }
          })
      }
      ,reject:function(obj) {
          layer.open({
              type: 2
              ,title: '驳回提现申请'
              ,content: layui.setter.webRoot + '/member/transfer_money_order/reject.html?tradeNo='+obj.data.tradeNo
              ,maxmin: false
              ,area: ['400px', '300px']
              ,btn: ['确定', '取消']
              ,yes: function(index, layero){
                  var iframeWindow = window['layui-layer-iframe'+ index]
                      ,submitID = 'LAY-form-submit'
                      ,submit = layero.find('iframe').contents().find('#'+ submitID);
                  iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
                      var field = data.field;
                      $.ajax({
                          type: "post",
                          url: layui.setter.webRoot + '/member/transfer_money_order/reject.do',
                          data:field,
                          dataType:"json",
                          success:function(res){
                              if(res.code == 200){
                                  table.reload('LAY-table');
                                  layer.close(index);
                              }else{
                                  layer.msg(res.msg,{icon: 2});
                              }
                          }
                      });
                  });
                  submit.trigger('click');
              }
          });
      }
  };
  
  $('.layui-btn.LAY-btn-fun').on('click', function(){
    var type = $(this).data('type');
    active[type] ? active[type].call(this) : '';
  });
  
  table.on('tool(LAY-table)', function(obj){
  	var type = obj.event ;
   	active[type] ? active[type].call(this,obj) : '';
  });
  
  exports('member_transfer_money_order_list', {})
});